import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

ApplicationWindow {
    id: authWindow
    width: 800
    height: 600
    visible: true
    title: qsTr("甘蔗农机智能管控平台 - 用户认证")
    color: "#f5f7fa"

    // 信号定义
    signal authenticationCompleted()

    // 当前页面状态：login / register / main
    property string currentPage: "login"

    // 页面切换动画
    property bool animating: false

    // 主界面加载器引用
    property var mainLoader: null

    // 初始化处理
    Component.onCompleted: {
        // 如果已经登录，直接显示主界面并调整窗口大小
        if (authManager.isLoggedIn) {
            authWindow.width = 1400
            authWindow.height = 900
            authWindow.title = qsTr("甘蔗农机智能管控平台 V1.0")
            authWindow.currentPage = "main"
        }
    }

    StackView {
        id: authStackView
        anchors.fill: parent

        // 根据登录状态决定初始页面
        initialItem: authManager.isLoggedIn ? mainInterfaceComponent : loginPageComponent

        // 页面切换动画
        pushEnter: Transition {
            PropertyAnimation {
                property: "x"
                from: authWindow.width
                to: 0
                duration: 300
                easing.type: Easing.OutCubic
            }
        }

        pushExit: Transition {
            PropertyAnimation {
                property: "x"
                from: 0
                to: -authWindow.width
                duration: 300
                easing.type: Easing.OutCubic
            }
        }

        popEnter: Transition {
            PropertyAnimation {
                property: "x"
                from: -authWindow.width
                to: 0
                duration: 300
                easing.type: Easing.OutCubic
            }
        }

        popExit: Transition {
            PropertyAnimation {
                property: "x"
                from: 0
                to: authWindow.width
                duration: 300
                easing.type: Easing.OutCubic
            }
        }
    }

    // 登录页面组件 - 使用直接的文件路径
    Component {
        id: loginPageComponent

        Loader {
            id: loginLoader
            source: "qrc:/pages/LoginPage.qml"

            onLoaded: {
                // 确保页面加载完成后再连接信号
                if (item) {
                    console.log("LoginPage loaded, connecting signals")
                    item.loginSuccess.connect(function () {
                        console.log("Login success signal received, switching to main interface")
                        authWindow.authenticationCompleted()
                        // 登录成功后直接跳转到主界面
                        loadMainInterface()
                    })

                    item.switchToRegister.connect(function () {
                        if (!authWindow.animating) {
                            authWindow.animating = true
                            authWindow.currentPage = "register"
                            authStackView.push(registerPageComponent)
                            animationTimer.start()
                        }
                    })
                }
            }
        }
    }
    
    // 注册页面组件 - 使用直接的文件路径
    Component {
        id: registerPageComponent

        Loader {
            id: registerLoader
            source: "qrc:/pages/RegisterPage.qml"

            onLoaded: {
                // 确保页面加载完成后再连接信号
                if (item) {
                    item.registerSuccess.connect(function () {
                        // 注册成功后自动切换到登录页面
                        if (!authWindow.animating) {
                            authWindow.animating = true
                            authWindow.currentPage = "login"
                            authStackView.pop()
                            animationTimer.start()
                        }
                    })

                    item.switchToLogin.connect(function () {
                        if (!authWindow.animating) {
                            authWindow.animating = true
                            authWindow.currentPage = "login"
                            authStackView.pop()
                            animationTimer.start()
                        }
                    })
                }
            }
        }
    }

    // 主界面组件
    Component {
        id: mainInterfaceComponent

        Loader {
            id: mainInterfaceLoader
            source: "qrc:/main.qml"

            onLoaded: {
                console.log("Main interface loaded successfully")
                // 主界面加载完成后，调整窗口大小和标题
                authWindow.width = 1400
                authWindow.height = 900
                authWindow.title = qsTr("甘蔗农机智能管控平台 V1.0")
                // 更新mainLoader引用
                authWindow.mainLoader = mainInterfaceLoader
                
                // 连接退出登录信号
                if (item && item.logoutRequested) {
                    item.logoutRequested.connect(function() {
                        console.log("Logout requested, returning to login page")
                        // 退出登录后返回到登录页面
                        authWindow.currentPage = "login"
                        authWindow.width = 800
                        authWindow.height = 600
                        authWindow.title = qsTr("甘蔗农机智能管控平台 - 用户认证")
                        authStackView.pop()
                    })
                }
            }
        }
    }

    // 动画状态控制定时器
    Timer {
        id: animationTimer
        interval: 350
        onTriggered: authWindow.animating = false
    }

    // 键盘快捷键
    Shortcut {
        sequence: "Escape"
        onActivated: {
            if (authStackView.depth > 1) {
                authStackView.pop()
                authWindow.currentPage = "login"
            }
        }
    }

    // 加载主界面函数
    function loadMainInterface() {
        console.log("loadMainInterface called, animating:", authWindow.animating)
        if (!authWindow.animating) {
            authWindow.animating = true
            authWindow.currentPage = "main"
            console.log("Pushing main interface component")
            
            // 使用平滑过渡效果切换到主界面
            authStackView.push(mainInterfaceComponent, {}, StackView.Transition)

            // 延迟一段时间后停止动画状态
            loadMainTimer.start()
        }
    }

    // 主界面加载定时器
    Timer {
        id: loadMainTimer
        interval: 500
        onTriggered: authWindow.animating = false
    }

    // 关闭窗口时的清理
    onClosing: {
        // 清理各个页面的状态
        if (authStackView.currentItem && typeof authStackView.currentItem.clearForm === "function") {
            authStackView.currentItem.clearForm()
        }
    }
}